package cn.livingCloud.service.impl;

import cn.livingCloud.context.Config;
import cn.livingCloud.dto.ImageUploadDto;
import cn.livingCloud.dto.front.FrontImageUploadDto;
import cn.livingCloud.service.ImageUploadFrontService;
import org.springframework.stereotype.Service;

/**
 * Created by DingJie on 2016/6/8.
 */
@Service
public class ImageUploadFrontServiceImpl extends ImageUploadServiceImpl implements ImageUploadFrontService {
    private static final String UPLOAD_RESULT_TEMPLATE = "<script>window.parent.%s({%s:\"%s\",state:%s});</script>";

    @Override
    public String saveUploadImage(FrontImageUploadDto imageUploadDto) {
        ImageUploadDto imageUploadDto1 = super.saveUploadImage(imageUploadDto);
        //callback({url:"xxx"})
        return String.format(UPLOAD_RESULT_TEMPLATE, imageUploadDto.getCallback(), "url"
                , Config.getDomainUrl() + "image/" + imageUploadDto1.getUrl(), true);
    }

    @Override
    public String buildErrorJson(FrontImageUploadDto imageFile) {
        return String.format(UPLOAD_RESULT_TEMPLATE, imageFile.getCallback(), "url"
                , "", false);
    }


}
